package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    /**
     * 会员档案列表查询
     */
    @Override
    public List<Member> findAll() {
        List<Member> list = memberDao.findAll();
        return list;
    }

    /**
     * 分页查会员信息
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询会员信息
        Page<Member> page = memberDao.selectByCondition(queryPageBean.getQueryString());
        //构建分页数据返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    //添加会员
    @Override
    public void add(Member member) {
        memberDao.add(member);
    }



    @Override
    public Member findById(int memberid) {
     Member member =  memberDao.findById(memberid);
     return member;
    }


    //更新会员信息
    @Override
    public void update(Member member) {
        //更新会员基本信息
        memberDao.update(member);
    }

    /*根据日期统计会员数,统计指定日期之前的会员数*/
    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for(String month:months){
            String date = month + ".31";
            Integer count= memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }
}
